﻿/******************************************************************************
* File description: SYPZModel.qml
* Function: 纯化单个实验流程控件
* Current Version: 1.1
* Author: 陈乃瑞
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.0
import QtQuick.Window 2.12
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls 2.12
import QtMultimedia 5.14
Rectangle {
    id:sypzmod
    width: parent.width
    height: 25*dp
    property int syid: 0
    property int btfl1: 0
    property int btfl2: 0
    property int btfl3: CExeConfig.getdef(syid)
    property string titlev: CExeConfig.rsyxx(syid)
    property int addnum: 0
    property int addtype: 0
    property int deltype: 0
    property int kjnum: 0
    property int idtype: 0
    property int onnb: 0

    signal clickcccc(string titleval,string numval,string textval,bool iszk)// 实验标题
    property var buttonData:CExeConfig.readsypz(syid)
    signal heichange();

    property var coorX: []      // 当前按钮坐标（每次拖动实时刷新）
    property var coorY: []
    property var onDrag: [false, false, false, false, false, false, false, false, false, false]
    property real dragX: 0      // 拖动的坐标
    property real dragY: 0
    property real biasX: 0      // 临时坐标，记录鼠标和按钮的偏移量
    property real biasY: 0
    property int num: buttonData.length
    property var dragStartX: 0  // 临时记录 点击开始的坐标
    property var dragStartY: 0
    property bool isPressed: false  // 临时记录 鼠标是否处于按下状态

    signal entersy(var sylcnum)
    signal delonnub(int syid,int btfl3)//删除当前实验流程
    signal copyonnub(int syid)//复制当前实验流程
    property string lsnumval: "" // 临时编号
    property string lstextval: "" // 临时类型

    CustomPopupBoxWarning{
        id: syjx1
        box_text: "是否删除?"
        onAccepted: {
            delonnub(syid,btfl3)
        }
    }
    CustomPopupBoxWarning{
        id: syjx2
        box_text: "是否复制?"
        onAccepted: {
            copyonnub(syid)
        }
    }

    // 坐标刷新
    function updateCoor(){
        coorX = []
        coorY = []
        for(let i = 0; i < num; i ++){
            if(onDrag[i]){
                coorX.push(dragX - biasX)
                coorY.push(dragY - biasY)
            }else{
                coorX.push(0)
                coorY.push(33*dp*i)
            }
        }
        for(let i = 0; i < num; i ++){
            buttonList.itemAt(i).x = coorX[i]
            buttonList.itemAt(i).y = coorY[i]

            if(onDrag[i]){
                buttonList.itemAt(i).z = 1
            }else{
                buttonList.itemAt(i).z = 0
            }
        }
    }
    Component.onCompleted: {
        updateCoor()
    }
    Connections{  // qmlre
        target: CExeConfig
        onUplist:{
            buttonData=CExeConfig.readsypz(syid)
            num= buttonData.length
            if(btfl1===1){
                sypzmod.height=buttonData.length*33*dp+25*dp
            }
        }
    }
    Connections{  // qmlre
        target: CExeConfig
        onUpdef:{
            btfl3=CExeConfig.getdef(syid)
        }
    }
    CustomPopupBoxWarning{
        id: syqd1
        showRejectedButton: false
        box_text: "请将其他实验流程设置为默认状态！"
    }
    Rectangle{
        id:myColumnLayout
        //        spacing: 10*dp
        Row{
            spacing: 5*dp
            CustomButton{
                id:bt1
                x:0*dp
                y:0*dp
                width: 20*dp
                height: 20*dp
                icon_size: 20*dp
                button_icon:btfl1 === 0? "qrc:/picture/pz_sq.png" : "qrc:/picture/pz_zk.png"
                button_icon_hover:btfl1===0?"qrc:/picture/pz_sq2.png":"qrc:/picture/pz_zk2.png"
                button_icon_press:btfl1===0?"qrc:/picture/pz_sq2.png":"qrc:/picture/pz_zk2.png"

                onReleased: {
                    if(btfl1===0){
                        button_icon= "qrc:/picture/pz_zk.png"
                        btfl1=1
                        sypzmod.height=buttonData.length*33*dp+25*dp
                        sylc.visible=true
                    }else{
                        button_icon= "qrc:/picture/pz_sq.png"
                        btfl1=0
                        sypzmod.height=25*dp
                        sylc.visible=false
                    }
                    clickcccc("实验信息","0","0",btfl1===1)
                }
                CustomTooltip{
                    visible: parent.isHovered
                    text: btfl1===0?"展开":"折叠"
                }
            }
            Rectangle{
                x:20*dp
                y:0*dp
                height: 20*dp
                width: 260*dp
                color: syid===onid?"#E6F0FF":"white"
                CustomButton{
                    visible: false
                    x:0*dp
                    y:0*dp
                    width: 25*dp
                    height: 20*dp
                    title: "默"
                    text_color: "white"
                    button_color: btfl3==1?"#1677FF":"#CCCCCC"
                    onReleased: {
                        if(btfl3===0){
                            CExeConfig.setdef(syid)
                        }else{
                            syqd1.show()
                        }
                    }
                    CustomTooltip{
                        visible: parent.isHovered
                        text: "默认"
                    }
                }

                Text {
                    id:syxx
                    x:0*dp
                    y:0*dp
                    //                y:2*dp
                    width: 170*dp
                    height: 20*dp
                    text: titlev
                    font.pixelSize: config.fontsize
                    verticalAlignment: Text.AlignVCenter
                    color: syid===onid?"blue":"black"
                    elide: Text.ElideRight
                    wrapMode: Text.NoWrap

                    MouseArea{
                        id: mouseArea
                        anchors.fill: parent
                        cursorShape: Qt.PointingHandCursor
                        onReleased: {
                            clickcccc("实验信息","0","0",btfl1===1)
                        }
                    }
                }
                CustomButton{
                    id:bt4
                    x:190*dp
                    y:3*dp
                    width: 14*dp
                    height: 14*dp
                    icon_size: 14*dp
                    button_icon: isHovered?"qrc:/picture/pz_sc2.png":"qrc:/picture/pz_sc.png"
                    button_icon_press:"qrc:/picture/pz_sc2.png"
                    button_color: syid===onid?"#E6F0FF":"white"
                    onReleased: {
                        syjx1.show()
                    }
                    CustomTooltip{
                        visible: parent.isHovered
                        text: "删除"
                    }
                }
                CustomButton{
                    id:fmbt5
                    x:210*dp
                    y:3*dp
                    width: 14*dp
                    height: 14*dp
                    icon_size: 14*dp
                    button_icon: isHovered?"qrc:/picture/pz_fz2.png":"qrc:/picture/pz_fz.png"
                    button_icon_press:"qrc:/picture/pz_fz2.png"
                    button_color: syid===onid?"#E6F0FF":"white"
                    onReleased: {
                        syjx2.show()
                    }
                    CustomTooltip{
                        visible: parent.isHovered
                        text: "复制"
                    }
                }
            }

        }
        Rectangle{
            id:sylc
            visible: false
            y:30*dp
            x:0*dp
            width: parent.width-35*dp
            Rectangle{
                x:7*dp
                width: 1*dp
                height: buttonData.length*33*dp
                color: "#DEDEDE"
            }


            Rectangle{
                id:layout
                x:15*dp
                width: parent.width-10*dp
                height: parent.height-30*dp
                Column{
                    id:colval
                    x:15*dp
                    width: parent.width-10*dp
                    height: parent.height-30*dp
                    spacing: 1*dp
                    Repeater{
                        id:buttonList
                        model: buttonData
                        delegate: Item {
                            width: 180 * dp
                            height: 32 * dp
                            NPushButton {
                                width: numval===onnb.toString()?190*dp:180*dp
                                x:numval===onnb.toString()?10*dp:15*dp
                                height: 32*dp
                                button_icon: modelData.icon
                                icon_width: 24 * dp
                                icon_height: 24 * dp
                                button_color: modelData.color
                                icon_color: {
                                    if(title==="等待"||title==="循环"||title==="跳过"||title==="暂停"){
                                        return "#333333"
                                    }else{
                                        return "white"
                                    }
                                }
                                text_color: "white"
                                title: modelData.title
                                textval: modelData.textval
                                numval: modelData.numval
                                textvalflag:true
                                onClickccc: {
                                    clickcccc(title, numval, textval,btfl1===1)
                                    onnb=Number(numval)
                                    lsnumval=numval
                                    lstextval=textval
                                }
                            }
                        }
                    }
                }
            }
            Item{
                height: buttonData.length*33*dp // 区域的高度
                width: 180*dp   // 区域的宽度
                MouseArea{
                    anchors.fill: parent
                    hoverEnabled: true
                    propagateComposedEvents: true
                    onEntered: {
                        console.log("鼠标进入", syid)
                        onid=syid
                        entersy(syid)
                        clickcccc(title,lsnumval,lstextval,true)
                    }
                    onPressed: {
                        mouse.accepted = false
                    }onReleased: {
                        mouse.accepted = false
                    }onPositionChanged: {
                        mouse.accepted = false
                    }
                }
                z: 100
            }
        }
    }
}
